let couponTimer = null;
export default {
  data() {
    return {
      couponCount: [], // 推荐商品倒计时
    };
  },
  beforeDestroy() {
    // 推荐商品倒计时
    clearInterval(couponTimer);
    couponTimer = null;
  },
  methods: {
    initCount(data) {
      // 推荐商品——倒计时批量处理
      // 倒计时清除
      clearInterval(couponTimer);
      couponTimer = null;

      this.couponCount = data.map(item => {
        const nowTime = new Date().getTime();
        return item.collar_end_time * 1000 -  nowTime;
      });

      // 暂时停止倒计时
      couponTimer = setInterval(this.initSelfCount, 1000);
    },
    initSelfCount() {
      // 推荐商品——递减时间戳
      const len = this.couponCount.length;
      for (let i = 0; i < len; i++) {
        if (this.couponCount[i] > 1000) {
          this.couponCount[i] = this.couponCount[i] - 1000;
        } else {
          this.couponCount[i] = 0;
        }
      }
      this.couponCount.splice(0, 0);
    },
  },
};
